<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客后台日志控制器负责，业务流程判断
 * 后台所有操作都需要权限，必需继承 CommonAction 控制器
 */
Class GetpostAction extends CommonAction{

	/**
	 * 处理全部日志首页操作
	 * @return [type] [description]
	 */
	Public function index(){
		$getlid = trim(G('get.lid'));
		$check = (!empty($getlid)) ? true : false;
		if ($check) {
			$M = M('Getpost')->showPost(G('get.lid'));
		} else {
			$M = M('Getpost')->showPost();
		}
		$this->assign('pfile', $M['post']);
		$this->assign('list', $M['list']);
		$this->assign('page', $M['page']);
		$this->assign('check', $check);
		$this->display();
	}

	/**
	 * 处理添加与修改日志操作
	 */
	Public function add(){
		M('Public')->putJsonFile();
		$geteid = trim(G('get.eid'));
		if (!empty($geteid)) {
			$file = unfile(C('post.postdata').G('get.eid').'.Mark');
			$this->assign('file', $file);
		} else {
			$this->assign('file', $file = array('listid' => '', 'ispl' => '1', 'tags' => array(''), 'id' => ''));
		}
		$this->assign('list', M('Getpost')->GPD());
		$this->display();
	}

	/**
	 * 处理POST数据是否正确操作
	 * @return [type] [description]
	 */
	Public function check(){
		if (ISPOST) {
			$postlistid = trim(G('post.listid'));
			$posttitle = trim(G('post.title'));
			$postcontent = trim(G('post.content'));
			if (empty($postlistid)) {
				$this->error('Shit, 我没有选择分类？');
			} elseif (empty($posttitle) || empty($postcontent)) {
				$this->error('Shit, 标题或内容空了？');
			}
			if (M('Getpost')->Add(G('post.'))) {
				$config = M('Public')->blogConfig();
				if ($config['cacheing']) {
					$this->clearCache('index-*');
					$this->clearCache('list-*');
					$getclearid = trim(G('post.id'));
					if (!empty($getclearid)) {
						$this->clearCache('post-'.$getclearid);
					}
				}
				$this->success('添加成功～！', U('index'));
			}
		}
	}

	/**
	 * 置顶日志首页
	 * @return [type] [description]
	 */
	Public function istop(){
		$this->assign('pfile', M('Getpost')->IsTOP());
		$this->display();
	}

	/**
	 * 处理日志置顶操作
	 * @return [type] [description]
	 */
	Public function setistop(){
		if (M('Getpost')->SetIsTOP(G('get.id'), G('get.lid'))) {
			$this->success('操作成功～！', U('istop'));
		} else {
			$this->error('操作失败～！(可能置顶日志已经有10篇～！)');
		}
	}

	/**
	 * 取消置顶操作
	 * @return [type] [description]
	 */
	Public function delistop(){
		if (M('Getpost')->DelIsTOP(G('get.id'))) {
			$this->success('操作成功～！', U('istop'));
		} else {
			$this->error('操作失败～！');
		}
	}

	/**
	 * 回收站首页
	 * @return [type] [description]
	 */
	Public function recindex(){
		$getlid = trim(G('get.lid'));
		$check = (!empty($getlid)) ? true : false;
		if ($check) {
			$M = M('Getpost')->RecoveryData(G('get.lid'));
		} else {
			$M = M('Getpost')->RecoveryData();
		}
		$this->assign('pfile', $M['post']);
		$this->assign('list', $M['list']);
		$this->assign('page', $M['page']);
		$this->assign('check', $check);
		$this->display();
	}

	/**
	 * 日志还原数据处理
	 * @return [type] [description]
	 */
	Public function replay(){
		if (G('get.check')) {
			if (M('Getpost')->PostRePlay()) {
				$this->success('还原成功～！', U('recindex'));
			} else {
				$this->error('还原失败～！');
			}
		} else {
			if (M('Getpost')->PostDel()) {
				$this->success('删除成功～！', U('recindex'));
			} else {
				$this->error('删除失败～！');
			}
		}
	}

	/**
	 * 处理日志回收操作
	 * @return [type] [description]
	 */
	Public function recovery(){
		if (M('Getpost')->rec()) {
			$this->success('回收成功～！', U('index'));
		} else {
			$this->error('回收失败~!');
		}
	}

	/**
	 * 指还原日志与删除操作
	 * @return [type] [description]
	 */
	Public function allreplay(){
		$postreplay = trim(G('post.replay'));
		$getpostid = G('post.id');
		if (empty($getpostid)) {
			$this->error('Shit，我有选择？');
		}
		if (!empty($postreplay)) {
			if (M('Getpost')->Allrep(true, G('post.'))) {
				$this->success('还原成功～！', U('recindex'));
			} else {
				$this->error('还原失败～！');
			}
		} else {
			if (M('Getpost')->Allrep(false, G('post.'))) {
				$this->success('删除成功～！', U('recindex'));
			} else {
				$this->error('删除失败～！');
			}
		}
	}

	/**
	 * 日志批量回收
	 * @return [type] [description]
	 */
	Public function allrecovery(){
		if (ISPOST) {
			if (M('Getpost')->AllRe(G('post.lid'), G('post.id'))) {
				$this->success('回收成功～！', U('index'));
			} else {
				$this->error('回收失败～！');
			}
		}
	}
}